package com.backedserver.controller.evaluate;

import com.common.base.BindCreateBy;
import com.common.base.BindUpdateBy;
import com.common.excel.ExcelUtil;
import com.common.excel.ExcelValueCheckResp;
import com.common.excel.excelinfo.EvaluateExportExcelInfo;
import com.common.global.BizException;
import com.common.global.CommonEnum;
import com.common.requestvo.evaluate.CheckEvaluateImportReqVo;
import com.common.requestvo.evaluate.CheckEvaluateReqVo;
import com.common.requestvo.evaluate.EvaluateInitReqVo;
import com.common.requestvo.target.TargetImportReqVo;
import com.common.service.evaluate.CheckEvaluateService;
import com.common.utils.JwtTokenUtils;
import com.common.utils.PageUtil;
import com.common.utils.ResultVoUtil;
import com.common.vo.PageVo;
import com.common.vo.ResultVo;
import com.common.vo.evaluate.CheckEvaluateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 指标导入Controller
 *
 * @author johnny
 * @create 2021-05-15 8:17 上午
 **/

@Api(tags = "司法业绩评价模块")
@RestController
@RequestMapping("/check_evaluate")
@Slf4j
public class CheckEvaluateController {


    private final CheckEvaluateService checkEvaluateService;

    public CheckEvaluateController(CheckEvaluateService checkEvaluateService) {
        this.checkEvaluateService = checkEvaluateService;
    }


    @ModelAttribute
    public TargetImportReqVo createByBind(TargetImportReqVo targetImportReqVo, HttpServletRequest request) {
        targetImportReqVo.setCreateBy(JwtTokenUtils.getUserNameFromReq(request));
        return targetImportReqVo;
    }

    /**
     * <p>
     * 根据文件类型，导入内容
     *
     * @param file
     * @return
     * @RequestParam("file") MultipartFile file
     */
    @PostMapping(value = "import")
    @ApiOperation(value = "导入考核结果评价信息")
    @BindCreateBy
    public ResultVo importCheckEvaluate(@RequestParam("file") MultipartFile file,
                                        CheckEvaluateImportReqVo checkEvaluateImportReqVo) {
        checkEvaluateImportReqVo.setFile(file);
        ExcelValueCheckResp excelValueCheckResp = checkEvaluateService.importFile(checkEvaluateImportReqVo);
        return ResultVoUtil.success(excelValueCheckResp);
    }


    @GetMapping("/listByCondition")
    @ApiOperation(value = "根据条件查询考核结果评价信息 (分页)")
    public ResultVo listByCondition(CheckEvaluateReqVo checkEvaluateReqVo,
                                    PageVo pageVo) {
        pageVo.setPageNumber(pageVo.getPageNumber() - 1);
        Page<CheckEvaluateVo> page = checkEvaluateService.listByCondition(checkEvaluateReqVo, PageUtil.initPage(pageVo));
        return ResultVoUtil.success(page);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增 考核结果评价信息")
    @BindCreateBy
    public ResultVo addEvaluate(@RequestBody CheckEvaluateReqVo checkEvaluateReqVo) {
        checkEvaluateService.add(checkEvaluateReqVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改 考核结果评价信息")
    @BindUpdateBy
    public ResultVo editEvaluate(@RequestBody CheckEvaluateReqVo checkEvaluateReqVo) {
        checkEvaluateService.edit(checkEvaluateReqVo);
        return ResultVoUtil.success();
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除 考核结果评价信息")
    @ApiImplicitParam(name = "id", value = "评价id", dataType = "Long", example = "1")
    public ResultVo deleteEvaluate(@PathVariable Long id) {
        checkEvaluateService.delete(id);
        return ResultVoUtil.success();
    }


    @PostMapping("/initEvaluateData")
    @ApiOperation(value = "根据条件 进行初始化 待业绩评价的 记录")
    @BindCreateBy
    public ResultVo initEvaluateData(@RequestBody EvaluateInitReqVo evaluateInitReqVo) {
        checkEvaluateService.initEvaluateData(evaluateInitReqVo);
        return ResultVoUtil.success();
    }


    @GetMapping("/export")
    @ApiOperation(value = "根据条件 导出 评价的 记录")
    public void exportExcel(CheckEvaluateReqVo checkEvaluateReqVo, HttpServletResponse response) {
        try {
            List<EvaluateExportExcelInfo> exportExcelInfos = checkEvaluateService.findForExcel(checkEvaluateReqVo);
            String fileName = "评价";
            ExcelUtil.writeExcel(response, exportExcelInfos, fileName, "评价sheet", new EvaluateExportExcelInfo());
        } catch (Exception e) {
            throw new BizException(CommonEnum.EXCEL_EXPORT_ERROR);
        }
    }
}